package com.waimai.mobile.ui.listener;

/**
 * Created by lin on 2015/5/30.
 */
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

import org.androidannotations.annotations.Click;

public class RecyclerItemClickListener implements View.OnTouchListener {
    private OnItemClickListener mListener;
    private GestureDetector mGestureDetector;

    public RecyclerItemClickListener(Context context, final RecyclerView recyclerView, OnItemClickListener listener) {

        mListener = listener;
        if(mListener != null){
            recyclerView.setOnTouchListener(this);
        }
        // 识别并处理手势
        mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {

            @Override public boolean onSingleTapUp(MotionEvent e) {
                View childView = recyclerView.findChildViewUnder(e.getX(), e.getY());
                if (childView != null && mListener != null) {
                    int position = recyclerView.getChildLayoutPosition(childView);
                    mListener.onItemClick(childView, position);
                }
                return true;
            }

            @Override public void onLongPress(MotionEvent e) {}
        });
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        return  mGestureDetector.onTouchEvent(motionEvent);
    }

    // 点击回调
    public interface OnItemClickListener {
        public void onItemClick(View view, int position);
        public void onItemLongClick(View view, int position);
    }
}
